#include <stdlib.h>

#include "lib.h"

#include "slotguts.h"
#include "heistguts.h"
#include "heistslotscommon.h"
#include "slotspin.h"
#include "getrand.h"

int32 g_safePayouts[] =
{
    100,
    110,
    130,
    140,
    250,
    270,
    280,
    290,
    210,
    220,
    230,
    250,
    260,
    270,
    290,
    300,
};


bool8 
HeistSlotGuts::isValidGuts()
{
    return getGutsId() >= 900 && getGutsId() < 1000;
}


void
HeistSlotGuts::spin(SlotSpin *slotSpin)
{
    SlotGuts::spin(slotSpin);
    SpinLine *spinLine = slotSpin->getSpinLineAt(0);
    int32 safePayoutIndex = -1;

    /**
     * Check the safe for cracking.
     */
    if ( spinLine->payouttypeid == HeistCommon::EVT_3DIAL )
    {
        safePayoutIndex = getrand() % (sizeof(g_safePayouts) / sizeof(g_safePayouts[0]));

		int32 averageBet = m_subGame->getData(HeistCommon::SGIDX_RUNNING_BET_TOTAL ) / m_subGame->getData(HeistCommon::SGIDX_RUNNING_BET_COUNT );
        m_subGame->update(HeistCommon::SGIDX_SAFECOUNTER, 0);
        m_subGame->update(HeistCommon::SGIDX_RUNNING_BET_TOTAL, 0);
        m_subGame->update(HeistCommon::SGIDX_RUNNING_BET_COUNT, 0);
				
		spinLine->payoutamount = (g_safePayouts[safePayoutIndex]  * averageBet * HeistCommon::SAFE_BONUS_MULTIPLIER * this->getInfo()->creditValue )/3;
 
	
	
	}
    else if ( m_subGame->getData(HeistCommon::SGIDX_SAFECOUNTER) >= HeistCommon::CRACKS_TO_OPEN )
    {
        safePayoutIndex = getrand() % (sizeof(g_safePayouts) / sizeof(g_safePayouts[0]));

		int32 averageBet = m_subGame->getData(HeistCommon::SGIDX_RUNNING_BET_TOTAL ) / m_subGame->getData(HeistCommon::SGIDX_RUNNING_BET_COUNT );
		m_subGame->update(HeistCommon::SGIDX_SAFECOUNTER, 0);
        m_subGame->update(HeistCommon::SGIDX_RUNNING_BET_TOTAL, 0);
        m_subGame->update(HeistCommon::SGIDX_RUNNING_BET_COUNT, 0);

        safePayoutIndex = getrand() % (sizeof(g_safePayouts) / sizeof(g_safePayouts[0]));
        spinLine->payoutamount = (g_safePayouts[safePayoutIndex] * averageBet * this->getInfo()->creditValue)/3;
    }
}

uint8
HeistSlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint8 result = HeistCommon::EVT_LOSER;
    int numGuns = 0;
    int numGlasses = 0;
    int numCoffees = 0;
    int numDonuts = 0;
    int numBadges = 0;
    int numDials = 0;

    //  Do a preliminary run-through
    for ( uint8 i = 0; i < getReelCount(); i++ )
    {
        // Set the icon flags;
        switch ( getIcon(i, stops[i])->iconid )
        {
            case HeistCommon::GUN:
                numGuns++;
                break;

            case HeistCommon::GLASSES:
                numGlasses++;
                break;

            case HeistCommon::COFFEE:
                numCoffees++;
                break;

            case HeistCommon::DONUT:
                numDonuts++;
                break;

            case HeistCommon::BADGE:
                numBadges++;
                break;

            case HeistCommon::DIAL:
                numDials++;
                break;
        }
    }

    //  EVT_3DIAL
    if ( numDials == 3 )
    {
        result = HeistCommon::EVT_3DIAL;
    }

    // EVT_3GLASSES
    if ( result == HeistCommon::EVT_LOSER && numGlasses == 3 )
    {
        result = HeistCommon::EVT_3GLASSES;
    }

    // EVT_COFFEES
    if ( result == HeistCommon::EVT_LOSER && numCoffees == 3 )
    {
        result = HeistCommon::EVT_3COFFEE;
    }

    // EVT_DONUT
    if ( result == HeistCommon::EVT_LOSER && numDonuts == 3 )
    {
        result = HeistCommon::EVT_3DONUT;
    }

    // EVT_3BADGE
    if ( result == HeistCommon::EVT_LOSER && numBadges == 3 )
    {
        result = HeistCommon::EVT_3BADGE;
    }

    // EVT_1GUN
    if ( result == HeistCommon::EVT_LOSER && numGuns == 1 )
    {
        result = HeistCommon::EVT_1GUN;
    }

    // EVT_2GUN
    if ( result == HeistCommon::EVT_LOSER && numGuns == 2 )
    {
        result = HeistCommon::EVT_2GUN;
    }

    // EVT_3GUN
    if ( result == HeistCommon::EVT_LOSER && numGuns == 3 )
    {
        result = HeistCommon::EVT_3GUN;
    }

    // EVT_1DIAL
    if ( result == HeistCommon::EVT_LOSER && numDials == 1 )
    {
        result = HeistCommon::EVT_1DIAL;
    }

    // EVT_2DIAL
    if ( result == HeistCommon::EVT_LOSER && numDials == 2 )
    {
        result = HeistCommon::EVT_2DIAL;
    }

    // EVT_3DIAL
    if ( result == HeistCommon::EVT_LOSER && numDials == 3 )
    {
        result = HeistCommon::EVT_3DIAL;
    }

	if(result != HeistCommon::EVT_3DIAL)
	{
		//update the number of dials in the subgamedata
        m_subGame->update(HeistCommon::SGIDX_SAFECOUNTER, m_subGame->getData(HeistCommon::SGIDX_SAFECOUNTER) + numDials);
	}


	//Update the running bet average (to adjust size of jackpot)
	int32 nRunningBetTotal = m_subGame->getData(HeistCommon::SGIDX_RUNNING_BET_TOTAL );
    int32 nRunningBetCount = m_subGame->getData(HeistCommon::SGIDX_RUNNING_BET_COUNT );

    nRunningBetTotal +=bet;
    ++nRunningBetCount;

    m_subGame->setServerOnly(HeistCommon::SGIDX_RUNNING_BET_TOTAL, true );
    m_subGame->update(HeistCommon::SGIDX_RUNNING_BET_TOTAL, nRunningBetTotal );
    m_subGame->setServerOnly(HeistCommon::SGIDX_RUNNING_BET_COUNT, true );
    m_subGame->update(HeistCommon::SGIDX_RUNNING_BET_COUNT, nRunningBetCount );

	return result;
}


